<template>
    <scroll-view class="message-list" :style="{ maxHeight: res.windowHeight -201 + 'px' }" scroll-y="true">
      <view v-for="(msg, index) in messages" :key="index" :class="['message-item', msg.isUser ? 'user-message' : 'ai-message']">
        <text class="message-text">{{ msg.text }}</text>
      </view>
    </scroll-view>
  <view class="container">
    <!-- 输入框 -->
    <view class="input-wrapper">
      <textarea
        v-model="inputValue"
        placeholder="询问任何问题..."
        :maxlength="500"
        class="input-area"
      />
      <view class="send-button" @click="sendMessage">
        <text v-if="!isSending">➤</text>
        <text v-else class="loading">⏳</text>
      </view>
    </view>

    <!-- 字数统计 -->
    <view class="character-count">{{ inputValue.length }}/500</view>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue"
import { getAiResponse } from '@/services/DeepSeekAI'

const inputValue = ref("")
const isSending = ref(false)
const messages = ref<{ text: string, isUser: boolean }[]>([]) // 存储消息列表
const res = uni.getSystemInfoSync()
const sendMessage = async () => {
  if (isSending.value || inputValue.value.trim() === "") return

  let message = inputValue.value
  messages.value.push({ text: message, isUser: true }) // **用户消息添加到列表**
  inputValue.value = "" // 清空输入框

  isSending.value = true // 开始加载动画
  try {
    const res = await getAiResponse(message)
    console.log("res:", res?.data);
    messages.value.push({ text: res?.data, isUser: false }) // **AI回复添加到列表**
  } catch (error) {
    console.error("请求失败", error)
    messages.value.push({ text: "AI: 发生错误，请重试", isUser: false }) // **错误提示**
  } finally {
    isSending.value = false // 恢复按钮状态
  }
}

</script>

<style lang="scss">
.container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 15px;
  background: #fff;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
/* 消息列表 */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;

  display: flex;
  flex-direction: column; /* 确保消息是从上到下排列 */
  justify-content: flex-end; /* 确保消息从底部开始显示 */
}

/* 消息项 */
.message-item {
  margin-top:10px;
  padding: 10px;
  border-radius: 8px;
  margin-bottom: 8px;
  max-width: 80%;
  word-wrap: break-word;
  display: flex;
  max-width: max-content;
  flex-direction: row;
  align-items: center;
}

/* 用户消息 - 靠右 */
.user-message {
  background: #007aff;
  //右对齐
  margin-left: auto;
}

/* AI消息 - 靠左 */
.ai-message {
  background: #f0f0f0;
  margin-right:auto
}

/* 文字 */
.message-text {
  font-size: 16px;
  word-break: break-word; /* 防止长字母打断 */
}


/* 输入框 */
.input-wrapper {
  position: relative;
  width: 100%;
}

.input-area {
  width: 100%;
  height: 150px;
  padding: 15px 60px 15px 15px;
  border: 1px solid #ccc;
  border-radius: 20px;
  font-size: 16px;
  box-sizing: border-box;
  resize: none;
  overflow-y: auto;
}

.send-button {
  position: absolute;
  right: 15px;
  bottom: 15px;
  width: 42px;
  height: 42px;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  user-select: none;
  background-color: #007aff !important;
}

.loading {
  font-size: 16px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.character-count {
  text-align: right;
  font-size: 12px;
  color: #888;
  margin-top: 5px;
}

</style>
